package com.qcu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcu.dto.RiskDto;
import com.qcu.entity.Archive;
import com.qcu.entity.ArchiveLog;
import com.qcu.excel.RiskExcel;
import com.qcu.mapper.ArchiveMapper;
import com.qcu.mapper.InhabitantMapper;
import com.qcu.mapper.UserMapper;
import com.qcu.service.ArchiveLogService;
import com.qcu.service.ArchiveService;
import com.qcu.util.Response;
import com.qcu.util.ThreadLocalUtil;
import com.qcu.vo.ArchiveOfUserVO;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
* @author MyComputer
* @description 针对表【archive】的数据库操作Service实现
* @createDate 2025-02-07 12:52:01
*/
@Service
public class ArchiveServiceImpl extends ServiceImpl<ArchiveMapper, Archive>
    implements ArchiveService{

    @Resource
    ArchiveMapper archiveMapper;
    @Resource
    ArchiveLogService archiveLogService;
    @Resource
    InhabitantMapper inhabitantMapper;
    @Resource
    UserMapper userMapper;

    @Override
    @Transactional
    public boolean saveArchive(Archive archive) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        archive.setStaffId(id);
        int insert = archiveMapper.insert(archive);
        if (insert > 0) {
            ArchiveLog archiveLog = new ArchiveLog();
            BeanUtils.copyProperties(archive, archiveLog);
            archiveLogService.save(archiveLog);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateByIdAndToLog(Archive archive) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        archive.setStaffId(id);

        int update = archiveMapper.updateById(archive);
        if (update > 0) {
            ArchiveLog archiveLog = new ArchiveLog();
            BeanUtils.copyProperties(archive, archiveLog);
            archiveLog.setId(null);
            archiveLogService.save(archiveLog);
            return true;
        }
        return false;
    }

    /**
     * 导出风险表到excel
     *
     * @param response
     */
    @Override
    public void export(HttpServletResponse response) {
        try {
            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = null;
            fileName = URLEncoder.encode("risk", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<RiskDto> risk = archiveMapper.getRisk();

            // 写出数据到excel
            ServletOutputStream outputStream = response.getOutputStream();
            EasyExcel.write(outputStream, RiskExcel.class).sheet("风险表").doWrite(risk);
        } catch (UnsupportedEncodingException e) {
            //todo: 抛出自定义异常
            throw new RuntimeException(e);
        } catch (IOException e) {
            //todo: 抛出自定义异常
            throw new RuntimeException(e);
        }

    }

    /**
     * 获取所有风险信息
     *
     * @return
     */
    @Override
    public Response getAllRisk() {
        List<RiskDto> risk = archiveMapper.getRisk();
        return Response.success(risk);
    }

    @Override
    public List<ArchiveOfUserVO> getArchiveOfUser(String username) {
        List<ArchiveOfUserVO> archiveOfUser = archiveMapper.getArchiveOfUser(username);
        return archiveOfUser;
    }
}




